using System;
using System.Collections.Generic;
using System.Text;

using DataSetGeneration;
using System.IO;
using System.Threading;

namespace DataSetGenerationConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*** Starting Generation ***");

            string outputDirectory = Config.OutputDirectory;
            string xsdExePath = Config.XsdexePath;

            string schemaFile = Config.SchemaFilePath;
            
            string server = Config.Server;
            string username = Config.Username;
            string password = Config.Password;

            string database = Config.Database;
            string namespaceName = Config.Namespace;

            DataSetGenerator g = new DataSetGenerator(server, username, password, database, xsdExePath);

            using (StreamReader sr = new StreamReader(schemaFile))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();

                    if (line.Trim().Length != 0)
                    {
                        string[] elements = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        if (elements.Length < 2)
                        {
                            Console.WriteLine("Failed to generate dataset - not enough input information: Check the definition file");
                            Console.WriteLine("Format = DataSetName, PrimaryTableName, OtherTableNames (all comma seperated)");
                        }
                        else
                        {
                            // First element in the line is the name of the dataset
                            // The next element is the dataset primary table name
                            // Then the other tables after that that are in the dataset
                            string dataSetName = elements[0].Trim();
                            string primaryTable = elements[1].Trim(); 

                            List<string> childTables = new List<string>();
                            for (int i = 2; i < elements.Length; i++)
                            {
                                childTables.Add(elements[i].Trim());
                            }

                            g.GenerateDataset(outputDirectory, dataSetName,
                                        namespaceName, primaryTable, childTables);

                            Console.WriteLine("Generated dataset: " + dataSetName);
                        }
                    }
                }
            }

            Console.WriteLine("*** Finished Generation ***");
            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
    }
}
